package com.hlx.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.admin.RailwayAuthority;
import com.hlx.service.admin.RailwayAuthorityService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/authority")
public class RailwayAuthorityController {

    @Autowired
    private RailwayAuthorityService railwayAuthorityService;

    @GetMapping("/getAuthority/{id}")
    public Result<RailwayAuthority> getAuthority(@PathVariable Integer id){
        RailwayAuthority railwayAuthority = railwayAuthorityService.getById(id);
        if(railwayAuthority.equals(null)){
            return Result.error("系统出现错误");
        }
        return Result.success(railwayAuthority);
    }

    @GetMapping("/getAllAuthority")
    public Result<List<RailwayAuthority>> getAllAuthority(){
        return Result.success(railwayAuthorityService.list());
    }

    @GetMapping("/getAllAuthorityByPage/{pageSize}/{currentPage}")
    public Result<Page> getAllAuthorityByPage(@PathVariable Integer pageSize,@PathVariable Integer currentPage){
        //        分页构造
        Page page = new Page(currentPage,pageSize);
        return Result.success(railwayAuthorityService.page(page));
    }

    @GetMapping("/getRailwayAuthorityByPage/{pageSize}/{currentPage}/{search}")
    public Result<Page> getAuthorityByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String search){
        //        分页构造
        LambdaQueryWrapper<RailwayAuthority> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(RailwayAuthority::getRailwayName,search);
        Page page = new Page(currentPage,pageSize);
        return Result.success(railwayAuthorityService.page(page,queryWrapper));
    }

    @PostMapping("/addRailwayAuthority")
    public Result<String> addRailwayAuthority(@RequestBody RailwayAuthority authority){
        if(railwayAuthorityService.save(authority)){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping("/putRailwayAuthority")
    public Result<String> putRailwayAuthority(@RequestBody RailwayAuthority authority){
        if(railwayAuthorityService.updateById(authority)){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @DeleteMapping ("/deleteRailwayAuthority/{id}")
    public Result<String> deleteRailwayAuthority(@PathVariable Integer id){
        if(railwayAuthorityService.removeById(id)){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
